<?php

//$code = verify::makeVerify(4,3);


class Verify {
						//定义属性
//图片宽高
protected $width;
protected $height;
//验证码方式、长度
protected $len;
protected $type;
//验证码数值
protected $code;
//存储画布
protected $image;



						//用静态方法自动new对象 制作验证码图片 并获得验证码值
	static public function makeVerify($len,$type=1,$width=100,$height=45) {
		$obj = new verify($len,$type,$width,$height);
		$obj -> paint();
		$obj -> sendImage();
		// session_start();
		// $_SESSION['yzm'] = $obj -> code;
		return $obj -> code;
	}



						//构造方法
	private function __construct($len,$type,$width,$height) {
		$this -> width = $width;
		$this -> height = $height;
		$this -> type = $type;
		if ($len > 3 && $len < 7) {
			$this -> len = (int)$len;
		}
		

	}




						//创建画布
//为画板分配颜色
	protected function getColor ($type=false) {
		$start = (int)$type*128;
		$end = $start + 127;

		$red = mt_rand($start,$end);
		$blue = mt_rand($start,$end);
		$green = mt_rand($start,$end);
		return imagecolorallocate($this -> image, $red, $green, $blue);
	}
//创建画布 并设置背景色
	protected function createImage() {
		$this -> image = imagecreatetruecolor($this -> width+10, $this -> height);

		$bgcolor = $this -> getColor(true);
		imagefill($this -> image,0,0,$bgcolor);
	}




						//产生验证码
		//选择类型
	protected function makeCode() {
		switch ($this -> type) {
			case '0':
				$str = $this -> number();
				break;
			case '2':
				$str = $this -> mix();
				break;
			case '3':
				$str = $this -> chinese();
				break;
			default:
				$str = $this -> alpha();
				break;
		}
		$this -> code = $str;                                           //最终验证码值
	}
//数字验证码
	protected function number() {
		$str = '1234567890';
		return substr(str_shuffle($str),0,$this -> len);
	}
//字母验证码
	protected function alpha() {
		$arr = array_merge(range('a','z'),range('A','Z'));
		shuffle($arr);
		return $str = substr(implode('',$arr),0,$this -> len);
	}
//混合验证码
	protected function mix() {
		$str = '';
		for ($i=0; $i < $this -> len; $i++) { 
			$type = mt_rand(0,2);
			switch ($type) {
				case '0':
					$str .= chr(mt_rand(ord(0),ord(9)));
					break;
				case '1':
					$str .= chr(mt_rand(ord('a'),ord('z')));
					break;
				case '2':
					$str .= chr(mt_rand(ord('A'),ord('Z')));
					break;
			}
		}
		return $str;
	}
//汉字验证码
	protected function chinese() {
		$str = '';
		for ($i=0; $i < $this->len; $i++) { 
			$ch1 = mt_rand(176,214);
			$ch2 = mt_rand(161,254);
			$str .= chr($ch1) . chr($ch2);
		}
		return iconv('gbk', 'utf-8', $str);
	}



						//将验证码画在画布上
	protected function paint() {
		$this -> createImage();
		$this -> makeCode();
		$this -> disturb();

		$fontsize = $this -> height / 1.5;
		$offsetY = ($this -> height + $fontsize) / 2;
		$perwidth = $this -> width/$this -> len;
		for ($i=0; $i < $this -> len ; $i++) { 
			$font = mb_substr($this -> code,$i,1);
			$angle = mt_rand(-30,30);
			$offsetX = $perwidth*$i + ($perwidth-$fontsize)/2;
			$offsetX = ($i == 0 ? $offsetX+10 : $offsetX);
			$color = $this -> getColor();
			imagettftext($this -> image,$fontsize,$angle,$offsetX,$offsetY,$color,'../fonts/lxkmht.ttf',$font);
		}
	}



	                    //设置干扰素
	protected function disturb() {
		for ($i = 1; $i < $this -> width; $i++) {
			$element = mt_rand(0,20);
			$pxX = mt_rand(0,$this -> width - 1);
			$pxY = mt_rand(0,$this -> height - 1);
			$Color = imagecolorallocate($this -> image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
	    //点干扰素
			if ($element < 18) {
				imagesetpixel($this -> image,$pxX,$pxY,$Color);
	    //  直线干扰素
			} else if ($element == 18) {
				imageline($this ->image,mt_rand(0,$this -> width-1),mt_rand(0,$this -> height-1),mt_rand(0,$this -> width-1),mt_rand(0,$this -> height-1),$Color);
	    //  曲线干扰素
			} else if ($element > 18) {
				imagearc($this -> image,mt_rand(0,$this -> width-1),mt_rand(0,$this -> height-1),$this -> width,$this -> height,mt_rand(0,90),mt_rand(0,90), $Color);
			}
		}
	}
	                    



	                    //发送图片
	protected function sendImage() {
		header('content-type:image/png');
		imagepng($this -> image);
		imagedestroy($this -> image);
	}
}



